package com.v2.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.v2.bean.Login;
import com.v2.request.RequestCenter;
import com.v2.utils.TSP;
import com.v2.utils.TToast;
import com.v2.utils.okhttp.OkHttpRequestCallback;
import com.zou.lotterylucky.Constants;
import com.zou.lotterylucky.R;
import com.zou.lotterylucky.activity.MainActivity;
import com.zou.lotterylucky.databinding.ActivityLoginBinding;
import com.zou.lotterylucky.utils.TLog;

public class LoginActivity extends BaseActivity<ActivityLoginBinding> {

    private final String[] SYSTEM_LANGUAGES = {"Spanish", "中文", "English"};

    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate() {
        handleEvents();
    }

    /*handle user events*/
    private void handleEvents() {
        //switch system language
        mBinding.switchLangLbl.setOnClickListener(view -> {
            int currentSystemLanguage = TSP.getInstance().getInt(Constants.SP_Lang, 1);
            QMUIDialog.CheckableDialogBuilder builder = new QMUIDialog.CheckableDialogBuilder(this);
            builder.setCheckedIndex(currentSystemLanguage);
            builder.addItems(SYSTEM_LANGUAGES, (dialog, which) -> {
                dialog.dismiss();
                TSP.getInstance().setInt(Constants.SP_Lang, which);
                Intent intent = new Intent(this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            });
            builder.create().show();
        });
        //login
        mBinding.loginBtn.setOnClickListener(view -> {
            String machine_number = mBinding.usernameTxt.getText().toString();
            String password = mBinding.passwordTxt.getText().toString();
            if (TextUtils.isEmpty(machine_number)) {
                TToast.show(R.string.input_username);
                return;
            }
            if (TextUtils.isEmpty(password)) {
                TToast.show(R.string.input_password);
                return;
            }
            login(machine_number, password);
        });
    }

    private void login(String machine_number, String password) {
        RequestCenter.login(machine_number, password, new OkHttpRequestCallback() {
            @Override
            public void onSuccess(String result) {
                TLog.net("appLogin(), success:" + result);
                TSP.getInstance().setBoolean(Constants.SP_REMEMBER_PASSWORD, mBinding.rememberPasswordCheck.isChecked());
                TSP.getInstance().setString(Constants.SP_USERNAME, machine_number);
                TSP.getInstance().setString(Constants.SP_PASSWORD, password);

                if (!new Gson().fromJson(result, Login.class).getData()) {
                    TToast.show(R.string.login_fail);
                    return;
                }

                LoginActivity.this.startActivity(new Intent(LoginActivity.this, MainActivity.class));
            }

            @Override
            public void onFailure(String error) {
                TLog.net("appLogin(), error:" + error);
                TToast.show("login failure:" + error);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TSP.getInstance().getBoolean(Constants.SP_REMEMBER_PASSWORD)) {
            mBinding.rememberPasswordCheck.setChecked(true);
            mBinding.usernameTxt.setText(TSP.getInstance().getString(Constants.SP_USERNAME));
            mBinding.passwordTxt.setText(TSP.getInstance().getString(Constants.SP_PASSWORD));
        } else {
            mBinding.usernameTxt.setText("");
            mBinding.passwordTxt.setText("");
        }
    }
}